<template>
  <div class="app-main-container" style="padding-top: 10px;">
    <el-tabs v-model="active" @tab-click="handleClick">
      <el-tab-pane v-for="(tab, i) in tabList" :key="tab.id" :label="tab.manufacturer" :name="i.toString()">
        <el-form ref="ruleform" :model="form" :rules="rules" label-suffix="：" label-width="auto">
          <el-form-item :label="$t('message.txxy')" prop="port">
            <el-select v-model="form.port" :placeholder="$t('message.qxz') + $t('message.txxy')" clearable class="w400">
              <el-option label="http" value="0" />
              <el-option label="https" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('message.txdz')" prop="ipAddre">
            <el-input v-model="form.ipAddre" :placeholder="$t('message.qsr') + $t('message.txdz')" class="w400" />
          </el-form-item>
          <el-form-item :label="$t('message.zh')" prop="username">
            <el-input v-model="form.username" :placeholder="$t('message.qsr') + $t('message.zh')" class="w400" />
          </el-form-item>
          <el-form-item :label="$t('message.password')" prop="password">
            <el-input
              v-model="form.password"
              :placeholder="$t('message.qsr') + $t('message.password')"
              type="password"
              class="w400"
              auto-complete="new-password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              v-preventReClick
              :loading="subLoading"
              type="primary"
              @click="onSubmit('ruleform')"
            >{{ $t('message.tj') }}</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane></el-tabs>
  </div>
</template>

<script>
import { manufacturerList, accountConfigs, accountConfigsInfo } from '@/api'
export default {
  name: 'AcquisitionManager',
  data() {
    return {
      subLoading: false,
      venueId: sessionStorage.getItem('venueId'),
      tabList: [],
      active: '0',
      rules: {
        ipAddre: [
          { required: true, message: this.$t('message.qsr') + this.$t('message.txdz'), trigger: 'blur' }
        ],
        port: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.txxy'), trigger: 'change' }
        ],
        username: [
          { required: true, message: this.$t('message.qsr') + this.$t('message.zh'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('message.qsr') + this.$t('message.password'), trigger: 'blur' }
        ]
      },
      form: {}
    }
  },
  mounted() {
    this.tomanufacturerList()
  },
  methods: {
    onSubmit(formName) {
      // const that = this
      const venueId = sessionStorage.getItem('venueId')
      this.$refs[formName][this.active].validate((valid) => {
        if (valid) {
          this.subLoading = true
          accountConfigs({
            param: {
              manufacturerId: this.tabList[this.active].id,
              venueId,
              accountParm: this.form
            }
          })
            .then(res => {
              this.subLoading = false
              if (res.errorCode === '000000') {
                this.$message.success(this.$t('message.tjcg'))
              }
            }).catch(err => {
              this.subLoading = false
              console.log(err)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    tomanufacturerList() {
      manufacturerList({ param: { type: '4' }}, sessionStorage.getItem('venueId'))
        .then(res => {
          this.tabList = res.recordList
          this.accountConfigsInfo()
        })
    },
    accountConfigsInfo() {
      const venueId = sessionStorage.getItem('venueId')
      accountConfigsInfo({ param: { manufacturerId: this.tabList[this.active].id, venueId }}, venueId).then(res => {
        this.form = res.record.accountParm || {}
      }).catch(err => {
        console.log(err)
      })
    },
    handleClick(e) {
      this.accountConfigsInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.tip {
  font-size: 12px;
  color: #666;
  line-height: 20px;
  height: 20px;
}
</style>
